Missing package
pip install pydub

brew install pydub

pip install git+https://github.com/jiaaro/pydub.git@master


In [28]:
#required libraries
import urllib
import scipy.io.wavfile
import pydub

In [38]:
#a temp folder for downloads
temp_folder="/Users/flavio.clesio/Downloads/audio_analysis/"
temp_folder


Out[38]:
'/Users/flavio.clesio/Downloads/audio_analysis/'

In [39]:
#spotify mp3 sample file
web_file="http://p.scdn.co/mp3-preview/35b4ce45af06203992a86fa729d17b1c1f93cac5"

In [40]:
#download file
urllib.urlretrieve(web_file,temp_folder+"file.mp3")
#read mp3 file
mp3 = pydub.AudioSegment.from_mp3(temp_folder+"file.mp3")
#convert to wav
mp3.export(temp_folder+"file.wav", format="wav")
#read wav file
rate,audData=scipy.io.wavfile.read(temp_folder+"file.wav")


Out[40]:
('/Users/flavio.clesio/Downloads/audio_analysis/file.mp3',
 <httplib.HTTPMessage instance at 0x10c0546c8>)

In [ ]:
# There's a bug in the code, reported in https://github.com/jiaaro/pydub/issues/189

#read mp3 file
mp3 = pydub.AudioSegment.from_mp3("Users/flavio.clesio/Downloads/file.mp3")

In [ ]:
#convert to wav
mp3.export(temp_folder+"file.wav", format="wav")

In [ ]:
#read wav file
rate,audData=scipy.io.wavfile.read(temp_folder+"file.wav")

In [ ]:
print(rate)

In [ ]:
print(audData)

In [ ]:
#wav length
audData.shape[0] / rate

In [ ]:
#wav number of channels mono/stereo
audData.shape[1]

In [ ]:
#if stereo grab both channels
channel1=audData[:,0] #left
channel2=audData[:,1] #right

In [ ]:
audData.dtype

In [ ]:
#save wav file
scipy.io.wavfile.write(temp_folder+"file2.wav", rate, audData)

In [ ]:
#save a file at half and double speed
scipy.io.wavfile.write(temp_folder+"file2.wav", rate/2, audData)
scipy.io.wavfile.write(temp_folder+"file2.wav", rate*2, audData)

In [ ]:
#save a single channel
scipy.io.wavfile.write(temp_folder+"file2.wav", rate, channel1)

In [ ]:
import numpy as np

In [ ]:
#averaging the channels damages the music
mono=np.sum(audData.astype(float), axis=1)/2
scipy.io.wavfile.write(temp_folder+"file2.wav", rate, mono)

In [ ]:
#Energy of music
np.sum(channel1.astype(float)**2)

In [ ]:
#power - energy per unit of time
1.0/(2*(channel1.size)+1)*np.sum(channel1.astype(float)**2)/rate

In [ ]:
import matplotlib.pyplot as plt

In [ ]:
#create a time variable in seconds
time = np.arange(0, float(audData.shape[0]), 1) / rate

In [ ]:
#plot amplitude (or loudness) over time
plt.figure(1)
plt.subplot(211)
plt.plot(time, channel1, linewidth=0.01, alpha=0.7, color='#ff7f00')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.subplot(212)
plt.plot(time, channel2, linewidth=0.01, alpha=0.7, color='#ff7f00')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.show()

In [ ]:
from numpy import fft as fft

In [ ]:
fourier=fft.fft(channel1)

In [ ]:
plt.plot(fourier, color='#ff7f00')
plt.xlabel('k')
plt.ylabel('Amplitude')

In [ ]:
n = len(channel1)
fourier = fourier[0:(n/2)]

In [ ]:
# scale by the number of points so that the magnitude does not depend on the length
fourier = fourier / float(n)

In [ ]:
#calculate the frequency at each point in Hz
freqArray = np.arange(0, (n/2), 1.0) * (rate*1.0/n);

In [ ]:
plt.plot(freqArray/1000, 10*np.log10(fourier), color='#ff7f00', linewidth=0.02)
plt.xlabel('Frequency (kHz)')
plt.ylabel('Power (dB)')

In [ ]:
plt.figure(2, figsize=(8,6))
plt.subplot(211)
Pxx, freqs, bins, im = plt.specgram(channel1, Fs=rate, NFFT=1024, cmap=plt.get_cmap('autumn_r'))
cbar=plt.colorbar(im)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
cbar.set_label('Intensity dB')
plt.subplot(212)
Pxx, freqs, bins, im = plt.specgram(channel2, Fs=rate, NFFT=1024, cmap=plt.get_cmap('autumn_r'))
cbar=plt.colorbar(im)
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
cbar.set_label('Intensity (dB)')
#plt.show()

In [ ]:
np.where(freqs==10034.47265625)
MHZ10=Pxx[233,:]
plt.plot(bins, MHZ10, color='#ff7f00')